package com.shj.em.biz.company.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.shj.em.biz.company.entity.Contact;
import com.shj.em.biz.company.mapper.ContactMapper;
import com.shj.em.biz.company.service.ContactService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.plugins.Page;

import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


/**
 * 联系人信息 服务实现类
 *
 * @author Shen Huang Jian
 * @since 2018-07-07
 */
@Service
@Transactional
public class ContactServiceImpl extends ServiceImpl<ContactMapper, Contact> implements ContactService {

    @Autowired
    ContactMapper contactMapper;

    public Page<Contact> queryContactPage(Map<String, Object> params){
        int offset = (Integer)params.get("offset");
        int pageSize = (Integer)params.get("limit");
        int pageNo = offset / pageSize + 1;
        Page<Contact> page = new Page(pageNo, pageSize);
        List<Contact> list = contactMapper.queryContactPage(page, params);
        return page.setRecords(list);
    }

    public List<Contact> findContactsByCompanyId(Long companyId){
        Wrapper<Contact> wrapper = new EntityWrapper<>();
        wrapper.where("company_id={0}", companyId)
                .andNew("del_flag={0}", 0);
        return contactMapper.selectList(wrapper);
    }

    public boolean isMobileExist(String mobileNo){
        Contact contact = contactMapper.findContactByMobile(mobileNo);
        return contact != null;
    }
}
